﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OnTheWay.Domain;
using OnTheWay.Interfaces;
using OnTheWay.Models;

namespace LocalService.Impl
{
    public class DomainImpl<T> : DomainCommon<T, BlackMofoServiceClient>, IObjectBasicBehaviors<T, Guid> where T : OnTheWay.Models.Model
    {
        /// <summary>
        /// 身份认证ID
        /// </summary>
        private Guid AuthIdValue = Guid.Empty;

        private Dictionary<Guid, T> mTestData = new Dictionary<Guid, T>();

        public bool Insert(T model)
        {
            if (mTestData != null)
            {
                if (!mTestData.ContainsKey(model.ID))
                {
                    mTestData.Add(model.ID, model);
                    return true;
                }
                else
                {
                    throw new Exception(model.GetType()+"[ID="+model.ID+"]已经存在");
                }
            }
            return this.Insert(AuthIdValue, model);
        }

        public bool Update(T model)
        {
            if (mTestData != null)
            {
                if (!mTestData.ContainsKey(model.ID))
                {
                    return false;//throw new Exception(model.GetType() + "[ID=" + model.ID + "]不存在");
                }
                else
                {
                    mTestData.Remove(model.ID);
                    mTestData.Add(model.ID, model);
                    return true;
                }
            }
            return this.Update(AuthIdValue, model);
        }

        public bool Delete(T model)
        {
            if (mTestData != null)
            {
                return mTestData.Remove(model.ID);
            }
            return this.Delete(AuthIdValue, model);
        }

        public T GetByID(Guid id)
        {
            if (mTestData != null)
            {
                if (!mTestData.ContainsKey(id))
                {
                    return null;
                }
                return mTestData[id];
            }
            return this.GetByID(AuthIdValue, id);
        }

        public bool UpdateList(IList<T> list)
        {
            if (mTestData != null)
            {
                bool ret = true;
                foreach(T model in list)
                {
                    ret = ret && this.Update(model);
                }
                return ret;
            }
            return this.UpdateList(AuthIdValue, list);
        }

        public bool Exists(Guid id)
        {
            if (mTestData != null)
            {
                return mTestData.ContainsKey(id);
            }
            return this.Exists(AuthIdValue, id);
        }

        public bool DeleteList(IList<T> list)
        {
            if (mTestData != null)
            {
                bool ret = true;
                foreach (T model in list)
                {
                    ret = ret && this.Delete(model);
                }
                return ret;
            }
            return this.DeleteList(AuthIdValue, list);
        }

        public bool DeleteByID(Guid id)
        {
            if (mTestData != null)
            {
                return mTestData.Remove(id);
            }
            return this.DeleteByID(AuthIdValue, id);
        }

        public long GetRecordCount(string where)
        {
            if (mTestData != null)
            {
                return mTestData.Count;
            }
            return this.GetRecordCount(AuthIdValue, where);
        }

        public IList<T> GetListByPage(string where, Page page)
        {
            if (mTestData != null)
            {
                List<T> list = new List<T>();
                foreach(T model in mTestData.Values) {
                    list.Add(model);
                }
                return list;
            }
            return this.GetListByPage(AuthIdValue, where, page);
        }

        public IList<T> GetListWhere(string where, string order, string asc)
        {
            if (mTestData != null)
            {
                List<T> list = new List<T>();
                foreach (T model in mTestData.Values)
                {
                    list.Add(model);
                }
                return list;
            }
            return this.GetListWhere(AuthIdValue, where, order, asc);
        }

        public IList<T> GetInValidData()
        {
            return this.GetInValidData(AuthIdValue);
        }

        public IList<T> GetInValidDataPage(Page page)
        {
            return this.GetInValidDataPage(AuthIdValue, page);
        }
    }
}
